/**
 * 
 */
package com.chocolatefordogs.writersnb.app;

import java.util.ResourceBundle;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;

import com.chocolatefordogs.writersnb.app.project.ProjectTree;
import com.chocolatefordogs.writersnb.main.MainWindowView;
import com.chocolatefordogs.writersnb.menu.MainMenu;
import com.chocolatefordogs.writersnb.notes.Note;
import com.chocolatefordogs.writersnb.projects.Project;
import com.chocolateforogs.writersnb.res.ResourceLoader;

/**
 * @author Matthew
 *
 */
public final class MainWindow implements MainWindowView {
	
	public MainWindow() {
		frame = new JFrame();
		loadResources();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
		
		
	    getTree().setRootVisible(false);
	    JScrollPane scroll = new JScrollPane(getTree());
	    frame.getContentPane().add(scroll, "Center");
	}
	
	private void loadResources() {
		ResourceBundle res = ResourceLoader.getClassResources(getClass());
		frame.setTitle(res.getString("title"));
	}
	
	public void show() {
		frame.setVisible(true);
	}
	
	public void close() {
		frame.setVisible(false);
		frame.dispose();
	}
	
	public void setMenu(final MainMenu menu) {
		frame.setJMenuBar(menu.getMenu());
	}
	
	private JTree getTree() {
		if (tree == null) {
			tree = new ProjectTree();
			
			final Project prj = new Project();
			prj.setTitle("Test Project");
			tree.addProject(prj);
			
			final Project prj2 = new Project();
			prj2.setTitle("Test Project 2");
			
			final Note p2n1 = new Note();
			p2n1.setTitle("P2N1");
			prj2.addNote(p2n1);
			
			tree.addProject(prj2);
		}
		
		return tree.getTree();
	}

	private final JFrame frame;
	private ProjectTree tree;
}
